/*
 * Copyright (C) 2013 rdrrlabs gmail com,
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/* grid control */

var timeriGCMTable;
var timeriGCMTableSelection;

$(document).ready(function() {
    timeriGCMTable = $("#timeriGCMTable").dataTable( {
        "sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'p>>",
        // http://www.datatables.net/usage/server-side
        "bServerSide": true,
        "bFilter ": false,
        "bSort ": false,
        "bSortClasses": false,
        "sAjaxSource": "/timer_gcm/get",
        "sPaginationType": "bootstrap",
        "aoColumns": [
            { "bSortable": false },
            { "bSortable": false },
            { "bSortable": false },
        ]
    })

    // Row selection: http://www.datatables.net/examples/api/select_single_row.html
    $("#timeriGCMTable tbody").click(function(event) {
		$(timeriGCMTable.fnSettings().aoData).each(function () {
            $(this.nTr).removeClass("row_selected")
		})
		var tr = event.target.parentNode
        $(tr).addClass("row_selected")
        timeriGCMOnRowSelected(timeriGCMTable.fnGetData(tr))
	})

    timeriGCMUnselectAll()
} )


// -----------------------------------------------------------

// No Add Dialog

// -----------------------------------------------------------

function timeriGCMRefresh() {
    timeriGCMUnselectAll()
    timeriGCMTable.fnDraw()
}

// -----------------------------------------------------------

function timeriGCMUnselectAll() {
    timeriGCMTableSelection = undefined;

    $(timeriGCMTable.fnSettings().aoData).each(function () {
        $(this.nTr).removeClass("row_selected")
    })

    var b = $("#timeriGCMDeleteButton")
    b.attr("disabled", "disabled");
    b.attr("title", "Select a row to delete it.")

    //--b = $("#timeriGCMAddButton")
    //--b.html("Add new user...")
}

function timeriGCMOnRowSelected(trGetData) {
    if (trGetData != undefined && trGetData.length == 2) {
        timeriGCMTableSelection = trGetData
        var b = $("#timeriGCMDeleteButton").removeAttr("disabled");
        b.attr("title", "Delete entry for email [" + timeriGCMTableSelection[0] + "]")

        //--b = $("#timeriGCMAddButton")
        //--b.html("Add/Edit user...")
    }
}

function timeriGCMDelete() {
    if (timeriGCMTableSelection == undefined) {
        alert("Error. Nothing to delete.")
        return
    }

    if (!confirm("Do you want to delete [" + timeriGCMTableSelection[0] + "] ?")) {
        return
    }

    var q = $.ajax({
        type: "DELETE",
        url:  "/timer_gcm/del/" + encodeURI(timeriGCMTableSelection[0]),
        error: function(xhr, status, error) {
            var str = $.trim(error + ": " + xhr.responseText.replace(/[^A-Za-z0-9]+/gi, " "));
            alert(str)
        },
        success: function(data, status, xhr) {
            timeriGCMRefresh()
        },
    })
}
